package dongshi.daddy.mbean;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

public class JdkMBeanTest {
    public static void main(String[] args) {
        /*RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        System.out.println("jvm名称：" + runtimeMXBean.getName());
        System.out.println("启动类加载器加载路径：" + runtimeMXBean.getBootClassPath());
        System.out.println("classpath: " + runtimeMXBean.getClassPath());
        System.out.println("启动时间（ms）：" + runtimeMXBean.getStartTime());
        System.out.println("运行时长(ms)：" + runtimeMXBean.getUptime());
        System.out.println("vm厂家：" + runtimeMXBean.getSpecVendor());
        System.out.println("vm版本：" + runtimeMXBean.getSpecVersion());
        System.out.println("系统属性：" + runtimeMXBean.getSystemProperties());*/

        /*MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        System.out.println("初始申请的堆内存大小：" + heapMemoryUsage.getInit());
        System.out.println("使用的堆内存大小：" + heapMemoryUsage.getUsed());
        System.out.println("最大堆内存大小：" + heapMemoryUsage.getMax());*/

        // ClassLoadingMXBean
        /*ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
        System.out.println("已加载的class数量：" + classLoadingMXBean.getLoadedClassCount());
        System.out.println("已卸载的class数量：" + classLoadingMXBean.getUnloadedClassCount());*/


        // GarbageCollectorMXBean
        // 这里返回多个的原因时，年轻代和老年代使用的垃圾收集器不同，如CMS 年轻代使用的时parnew，年老代使用的CMS
        List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean garbageCollectorMXBean : garbageCollectorMXBeans) {
            System.out.println("垃圾收集器的名称：" + garbageCollectorMXBean.getName());
            System.out.println("执行GC的次数：" + garbageCollectorMXBean.getCollectionCount());
            System.out.println("执行GC的时长：" + garbageCollectorMXBean.getCollectionTime());
        }
/*
        Instrumentation instrumentation = new InstrumentationImpl();
        System.out.println(instrumentation.getObjectSize(new Integer(1)));*/
    }
}
